package javabasic.slipch.prime;
import java.util.Scanner;
import java.util.InputMismatchException;

import javabasic.slipch.prime.PrimeNumbersList;

public final class Main {

	public static void main(String[] args) {
	
		int maxNumber = input();
		
		System.out.println("\nThe prime numbers less or equal " + maxNumber + " are: \n");
		Iterable<Integer> primeNumbers = new PrimeNumbersList( maxNumber );
		System.out.print("[");
		for( int prime : primeNumbers ) {
			System.out.print(prime + ",");
		}
		System.out.print("]");
	}
	
	private static int input() {
		Scanner inputReader = new Scanner(System.in);
		int maxNumber = - 1;
		while (true) {
			System.out.print(PROMPT);
			try {
				maxNumber = inputReader.nextInt();
				if ( maxNumber > 0) {
					break;
				} else {
					System.out.print(ERROR_MESSAGE);
					inputReader.next();
				}
			} catch (InputMismatchException e) {
				System.out.print(ERROR_MESSAGE);
				inputReader.next();
			}
		};
		
		return maxNumber;
	}
	private static final String ERROR_MESSAGE= "\nShould be a natural number \n";
	private static final String PROMPT= "Enter a natural number: ";
}
